FROM openjdk:8
# Debian 11 (bullseye)

# 配置环境变量; 相当于修改[/etc/profile]
# 否则安装时报错,找不到[leptonica,tesseract]等
ENV LD_LIBRARY_PATH="/usr/local/lib" \
    LIBLEPT_HEADERSDIR="/usr/local/include" \
    PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"

# 将外部压缩包 解压到容器根目录
# http://www.leptonica.org/source/leptonica-1.80.0.tar.gz
# https://github.com/tesseract-ocr/tesseract/archive/5.3.3.tar.gz
ADD leptonica-1.80.0.tar.gz /
ADD tesseract-5.3.3.tar.gz /

# 更换[镜像源] 解决[apt-get update]网速慢
# https://www.cnblogs.com/liuguanglin/p/debian11_repo.html
RUN echo "\
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free\n\
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free\n\
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free\n\
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free\n\
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free\n\
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free\n\
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free\n\
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free\n" > /etc/apt/sources.list

# 准备编译环境,安装库
# 文件类型依赖 https://tesseract-ocr.github.io/tessdoc/InputFormats
# 可能有Debian非必要的库,不同linux的库会有不同(如libstdc++, build-essential),具体找ai
# [4核 2G]耗时6min, make单核时15min
RUN apt-get update \
    && apt install -y libstdc++6 build-essential \
    && apt install -y autoconf automake libtool \
    && apt install -y libpng-dev zlib1g-dev libjpeg-dev libtiff-dev \
    && apt install -y libicu-dev uuid-dev \
    && apt install -y g++ make cmake \
    && apt install -y pkg-config \
    && cpu_num=$(grep -c '^processor' /proc/cpuinfo) \
    && cd /leptonica-1.80.0 && ./configure && make -j${cpu_num} && make install \
    && cd /tesseract-5.3.3 && ./autogen.sh && ./configure && make -j${cpu_num} && make install && ldconfig \
    && rm -rf /leptonica-1.80.0 /tesseract-5.3.3 \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# 构建命令
# 使用tesseract的版本号代替镜像版本号
# 大小1.3G; jdk526M
# docker build \
# --tag hup/jdk_tess:5.3.3 \
# .;

# 测试
# mkdir -p /docker/tesseract/config
# 复制 _tessdata, 打包/部署jar,yml
# docker run -d \
# -p 8080:8080 \
# -v /docker/tesseract:/docker/tesseract \
# -v /etc/localtime:/etc/localtime:ro \
# -e TZ=Asia/Shanghai \
# -e JAVA_OPTS="-Dfile.encoding=UTF-8" \
# -w /docker/tesseract \
# --name tess \
# hup/jdk_tess:5.3.3 \
# java -jar /docker/tesseract/Tesseract-0.0.1.jar

# 镜像文件 导入导出
# 注意文件名别用冒号,linux支持,windows不支持
# docker save -o 导出文件名 镜像名:版本
# docker load -i 导入文件名
# docker save -o hup_jdk_tess_5.3.3.tar hup/jdk_tess:5.3.3
# docker load -i hup_jdk_tess_5.3.3.tar
